<?php
#class errlog
#{
#    private $on;
#    private $sock;
#
#    public function __construct()
#    {/*{{{*/
#        $this->on=true;
#        $this->sock=null;
#        if(($location=get_cfg_var("errlog_location")))
#        {
#            $addr=explode(":", str_replace("//", "", $location));
#            $ip=gethostbyname($addr[0]);
#            $port=intval($addr[1]);
#            $this->sock=socket_create(AF_INET, SOCK_DGRAM, 0); 
#            if(socket_connect($this->sock, $ip, $port) == false)
#            {
#                socket_close($this->sock);
#                $this->sock=null;
#            }
#        }
#    }/*}}}*/
#
#    public function __destruct()
#    {/*{{{*/
#        if($this->sock)
#        {
#            socket_close($this->sock);
#        }
#    }/*}}}*/
#
#    public function turnon()
#    {/*{{{*/
#        $this->on=true;
#    }/*}}}*/
#
#    public function turnoff()
#    {/*{{{*/
#        $this->on=false;
#    }/*}}}*/
#
#    public function write()
#    {/*{{{*/
#        if($this->on && $this->sock)
#        {
#            $args=func_get_args();
#            $argsnum=func_num_args();
#            if($argsnum > 0)
#            {
#                $fmt=$args[0];
#                for($i=0; $i<$argsnum; ++$i)
#                    $args[$i]=$args[$i+1];
#                $msg=vsprintf($fmt, $args);
#                $len=para::put_item($sendbuf, "LOG", "httpd", $msg);
#                $num=socket_select($read=null,
#                        $write=array($this->sock), $exp=null,
#                        5, 0);
#                if($num > 0)
#                    socket_send($this->sock, $sendbuf, $len, 0); 
#            }
#        }
#    }/*}}}*/
#}
class errlog
{
    public static function write()
    {/*{{{*/
        $location=get_cfg_var("errlog_location");
        if(!$location)
        {
            return false;
        }

        $addr=explode(":", str_replace("//", "", $location));
        $ip=gethostbyname($addr[0]);
        $port=intval($addr[1]);
        $sock=socket_create(AF_INET, SOCK_DGRAM, 0); 
        if(socket_connect($sock, $ip, $port) == false)
        {
            socket_close($sock);
            return false;
        }

        $args=func_get_args();
        $argsnum=func_num_args();
        if($argsnum > 0)
        {
            $fmt=$args[0];
            for($i=0; $i<$argsnum; ++$i)
                $args[$i]=$args[$i+1];
            $msg=vsprintf($fmt, $args);
            $len=para::put_item($sendbuf, "LOG", "httpd", $msg);
            $num=socket_select($read=null, $write=array($sock),
                    $exp=null, 5, 0);
            if($num > 0)
                socket_send($sock, $sendbuf, $len, 0); 
        }
        socket_close($sock);
        return true;
    }/*}}}*/
}
?>
